% Basic Introduction:
% This function transforms a long sparse matrix (size=[D1_1*D1_2,D2]) 
% to a wide matrix (size=[D1_2,D1_1*D_2]).
% This transformation can facilitate the expectation computation 
% significantly.
% M_wide=LongToWide(M_long,[D1_1,D1_2,D2]')
% Example:
% If size(A_i)=[D1_2,D2] and D1_1=2, then this function will transform
% [A_1;A_2] to [A_1,A_2].
function [M_wide,Wi,Wj,Val]=SpaFun_Long2Wide(M_long,DimInfo)

D1_1            =   DimInfo(1);
D1_2            =   DimInfo(2);
D2              =   DimInfo(3);

[Li,Lj,Val]     =   find(M_long);

multiple        =   ceil(Li/D1_2)-1;

Wi              =   Li-multiple*D1_2;
Wj              =   Lj+multiple*D2;

M_wide          =   sparse(Wi,Wj,Val,D1_2,D1_1*D2);
